<template>
    <div>
        <h1>添加会员</h1>
        <table>
            <tbody>
                <tr>
                    <td>会员编号</td>
                    <td>
                        <input type="text" disabled v-model="date.memberId">
                    </td>
                </tr>
                <tr>
                    <td>会员名称</td>
                    <td>
                        <input type="text" v-model="date.memberName">
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="upload">
                        <img :src="'https://localhost:7258/'+date.img" width="100px" height="100px">
                    </td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td>
                        <input type="text" v-model="date.age">
                    </td>
                </tr>
                <tr>
                    <td>会员等级编号</td>
                    <td>
                        <select v-model="date.memberLevelId">
                            <option value="0">请选择</option>
                            <option v-for="a in data" :value="a.memberLevelId">{{ a.memberLevelName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="checkbox" v-model="date.state">勾选为true,否则false
                    </td>
                </tr>
                <tr>
                    <td>是否删除</td>
                    <td>
                        <input type="checkbox" v-model="date.mSdelete">勾选为true,否则false
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2">
                        <input type="button" value="新增" @click="update">
                    </td>
                </tr>
            </tbody>
        </table>
        <a href="/ShowMember">返回显示列表</a>
    </div>
</template>

<script setup lang="ts">
import { useRouter,useRoute } from 'vue-router';
import { ref,onMounted } from 'vue';
import axios from 'axios';
const routher = useRouter()
const routhe = useRoute()
onMounted(()=>{
    getdate(),
    xiu()
})
const getdate=()=>{
    axios.get("https://localhost:7258/api/MemerLevel/GetMemberLevels",{params:data.value}).then(res=>{
        data.value=res.data;
    })
}
const data=ref([{
    "memberLevelId": 0,
    "memberLevelName": "",
    "lstate": false,
    "rightLevelId": 0
}])
const upload=(e:any)=>{
    var a = e.target.files[0]
    var aa = new FormData()
    aa.append("file",a)

    if(a.size>2*1024*1024){
        alert("大小不能超过2MB")
        return
    }
    if(a.type!="image/jpeg"&&a.type!="image/png"){
        alert("格式只能是jpg,png")
        return
    }
    axios.post("https://localhost:7258/api/UpLoad/UpFile",aa).then(res=>{
        date.value.img=res.data;
        alert("上传成功")
    })
}
const xiu=()=>{
    var vid = routhe.query.id
    axios.get("https://localhost:7258/api/Member/GetMember",{params:{id:vid}}).then(res=>{
        date.value=res.data
    })
}
const update=()=>{
    axios.put("https://localhost:7258/api/Member/Xmember",date.value).then(res=>{
        if(res.data>0){
            alert("更新会员成功")
            routher.push({path:"/ShowMember"})
        }
        else{
            alert("更新会员失败")
        }
    })
}
const date=ref({
  "memberId": 0,
  "memberName": "",
  "img": "",
  "age": 0,
  "memberLevelId": 0,
  "state": false,
  "mSdelete": false
})
</script>

<style scoped>

</style>